package com.iknow.http.parser;

import android.text.Spannable;
import android.text.SpannableStringBuilder;
import com.iknow.IKnowApiV4;
import com.iknow.ui.model.IKPageAdapter;
import com.iknow.util.SpannableUtil;
import com.iknow.util.StringUtil;
import com.iknow.util.enums.IKTagEnum;
import com.iknow.xml.IKValuePageData.IKValuePageData_Item;
import java.net.URL;
import java.util.Map;
import org.htmlparser.Node;
import org.htmlparser.Parser;
import org.htmlparser.lexer.Lexer;
import org.htmlparser.nodes.TagNode;
import org.htmlparser.nodes.TextNode;
import org.htmlparser.tags.BodyTag;
import org.htmlparser.tags.Html;
import org.htmlparser.tags.ImageTag;
import org.htmlparser.tags.LinkTag;
import org.htmlparser.tags.ParagraphTag;
import org.htmlparser.tags.Span;
import org.htmlparser.util.NodeList;
import org.htmlparser.util.ParserException;

public class HtmlParser
{
  private final String AUDIO_TEXT;
  private final String TAG = "HtmlParser";
  private IKPageAdapter mAdapter;
  private IKValuePageData.IKValuePageData_Item mCurrentDataItem;
  private Parser mHtmlParser;

  public HtmlParser(IKPageAdapter paramIKPageAdapter)
  {
    this.AUDIO_TEXT = "对不起,您的浏览器不支持播放音频";
    this.mAdapter = paramIKPageAdapter;
  }

  public HtmlParser(URL paramURL)
  {
    this.AUDIO_TEXT = "对不起,您的浏览器不支持播放音频";
  }

  private void addText(TextNode paramTextNode)
  {
    String str = clearText(paramTextNode.getText());
    if (StringUtil.isEmpty(str));
    while (true)
    {
      SpannableStringBuilder localSpannableStringBuilder;
      while (true)
      {
        return;
        localSpannableStringBuilder = new SpannableStringBuilder(str);
        if (this.mCurrentDataItem == null)
          break;
        Spannable localSpannable = SpannableUtil.mergeSpanna((Spannable)this.mCurrentDataItem.data.get("text"), localSpannableStringBuilder);
        this.mCurrentDataItem.data.remove("text");
        this.mCurrentDataItem.data.put("text", localSpannable);
      }
      this.mCurrentDataItem = new IKValuePageData.IKValuePageData_Item();
      this.mCurrentDataItem.type = IKTagEnum.TEXT;
      this.mCurrentDataItem.data.put("text", localSpannableStringBuilder);
    }
  }

  private void checkTextNode()
  {
    if (this.mCurrentDataItem != null);
    try
    {
      this.mAdapter.addHtmlNode(this.mCurrentDataItem.clone());
      this.mCurrentDataItem.data.clear();
      this.mCurrentDataItem = null;
      return;
    }
    catch (CloneNotSupportedException localCloneNotSupportedException)
    {
      localCloneNotSupportedException.printStackTrace();
    }
  }

  private String clearText(String paramString)
  {
    String str;
    if ((paramString != null) && (paramString.equalsIgnoreCase("对不起,您的浏览器不支持播放音频")))
      str = null;
    while (true)
    {
      return str;
      str = StringUtil.formatHtmlToText(paramString.replace("\n", ""));
    }
  }

  private void parseAudioTag(TagNode paramTagNode)
  {
    if (paramTagNode.isEndTag());
    while (true)
    {
      return;
      IKValuePageData.IKValuePageData_Item localIKValuePageData_Item = new IKValuePageData.IKValuePageData_Item();
      localIKValuePageData_Item.type = IKTagEnum.AUDIO;
      String str = paramTagNode.getAttribute("src");
      if (str == null)
      {
        Node localNode = paramTagNode.getNextSibling();
        if ((localNode != null) && (localNode instanceof TagNode))
          str = ((TagNode)localNode).getAttribute("src");
      }
      localIKValuePageData_Item.data.put("src", str);
      this.mAdapter.setAudioDataItem(localIKValuePageData_Item);
      checkTextNode();
    }
  }

  private void parseBodyNode(Node paramNode)
  {
    int i;
    if (paramNode.getChildren() == null);
    do
    {
      return;
      i = 0;
    }
    while (i >= paramNode.getChildren().size());
    Node localNode = paramNode.getChildren().elementAt(i);
    if (localNode instanceof ParagraphTag)
      parsePTag((ParagraphTag)localNode);
    while (true)
    {
      while (true)
        ++i;
      parseChildNode(localNode);
    }
  }

  private void parseBrTag(TagNode paramTagNode)
  {
    if (paramTagNode.isEndTag());
    while (true)
    {
      SpannableStringBuilder localSpannableStringBuilder;
      while (true)
      {
        return;
        localSpannableStringBuilder = new SpannableStringBuilder("\n");
        if (this.mCurrentDataItem == null)
          break;
        Spannable localSpannable = SpannableUtil.mergeSpanna((Spannable)this.mCurrentDataItem.data.get("text"), localSpannableStringBuilder);
        this.mCurrentDataItem.data.remove("text");
        this.mCurrentDataItem.data.put("text", localSpannable);
      }
      this.mCurrentDataItem = new IKValuePageData.IKValuePageData_Item();
      this.mCurrentDataItem.type = IKTagEnum.TEXT;
      this.mCurrentDataItem.data.put("text", localSpannableStringBuilder);
    }
  }

  private void parseChildNode(Node paramNode)
  {
    if (paramNode instanceof ImageTag)
      parseImageTag((ImageTag)paramNode);
    while (true)
    {
      TagNode localTagNode;
      do
        while (true)
        {
          while (true)
          {
            do
              while (true)
              {
                while (true)
                {
                  while (true)
                  {
                    return;
                    if (!(paramNode instanceof LinkTag))
                      break;
                    parseLinkTag((LinkTag)paramNode);
                  }
                  if (!(paramNode instanceof Span))
                    break;
                  parseSpanTag((Span)paramNode);
                }
                if (!(paramNode instanceof TextNode))
                  break;
                addText((TextNode)paramNode);
              }
            while (!(paramNode instanceof TagNode));
            localTagNode = (TagNode)paramNode;
            if (!(localTagNode.getTagName().equalsIgnoreCase("audio")))
              break;
            parseAudioTag(localTagNode);
          }
          if (!(localTagNode.getTagName().equalsIgnoreCase("video")))
            break;
          parseVedioTag(localTagNode);
        }
      while (!(localTagNode.getTagName().equalsIgnoreCase("br")));
      parseBrTag(localTagNode);
    }
  }

  private void parseHtmlNode(Html paramHtml)
  {
    int i;
    if (paramHtml.getChildren() != null)
    {
      i = 0;
      if (i < paramHtml.getChildCount())
        break label18;
    }
    return;
    label18: Node localNode = paramHtml.getChild(i);
    if (localNode instanceof BodyTag)
    {
      parseBodyNode(localNode);
      checkTextNode();
    }
    while (true)
    {
      while (true)
      {
        while (true)
          ++i;
        if (!(localNode instanceof ParagraphTag))
          break;
        parsePTag((ParagraphTag)localNode);
      }
      parseChildNode(localNode);
    }
  }

  private void parseImageTag(ImageTag paramImageTag)
  {
    IKValuePageData.IKValuePageData_Item localIKValuePageData_Item = new IKValuePageData.IKValuePageData_Item();
    localIKValuePageData_Item.type = IKTagEnum.IMAGE;
    String str = paramImageTag.getAttribute("src");
    if (str.indexOf("http://") == -1)
      str = IKnowApiV4.getResizeContentImageUrl(str);
    localIKValuePageData_Item.data.put("src", str);
    localIKValuePageData_Item.data.put("height", paramImageTag.getAttribute("height"));
    localIKValuePageData_Item.data.put("width", paramImageTag.getAttribute("width"));
    this.mAdapter.addHtmlNode(localIKValuePageData_Item);
    checkTextNode();
  }

  private void parseLinkTag(LinkTag paramLinkTag)
  {
    String str1 = paramLinkTag.getAttribute("href");
    String str2 = paramLinkTag.getText();
    if (paramLinkTag.getChildCount() != 0)
      str2 = paramLinkTag.getChildren().elementAt(0).getText();
    Spannable localSpannable1 = SpannableUtil.getHyperLink(str1, str2);
    if (this.mCurrentDataItem == null)
    {
      this.mCurrentDataItem = new IKValuePageData.IKValuePageData_Item();
      this.mCurrentDataItem.type = IKTagEnum.TEXT;
      this.mCurrentDataItem.data.put("text", localSpannable1);
    }
    while (true)
    {
      return;
      Spannable localSpannable2 = SpannableUtil.mergeSpanna((Spannable)this.mCurrentDataItem.data.get("text"), localSpannable1);
      this.mCurrentDataItem.data.remove("text");
      this.mCurrentDataItem.data.put("text", localSpannable2);
    }
  }

  private void parsePTag(ParagraphTag paramParagraphTag)
  {
    int i;
    if (paramParagraphTag.getChildren() != null)
      i = 0;
    while (true)
    {
      if (i >= paramParagraphTag.getChildren().size())
      {
        checkTextNode();
        return;
      }
      parseChildNode(paramParagraphTag.getChild(i));
      ++i;
    }
  }

  private void parseSpanTag(Span paramSpan)
  {
    int i;
    if (paramSpan.getChildren() != null)
      i = 0;
    while (true)
    {
      if (i >= paramSpan.getChildren().size())
        return;
      parseChildNode(paramSpan.getChildren().elementAt(i));
      ++i;
    }
  }

  private void parseVedioTag(TagNode paramTagNode)
  {
    if (paramTagNode.isEndTag());
    while (true)
    {
      TagNode localTagNode;
      do
      {
        Node localNode;
        do
        {
          return;
          localNode = paramTagNode.getNextSibling();
        }
        while (localNode == null);
        localTagNode = null;
        if (!(localNode instanceof TagNode))
          break;
        localTagNode = (TagNode)localNode;
      }
      while (!(localTagNode.getTagName().equalsIgnoreCase("embed")));
      IKValuePageData.IKValuePageData_Item localIKValuePageData_Item = new IKValuePageData.IKValuePageData_Item();
      localIKValuePageData_Item.type = IKTagEnum.VIDEO;
      localIKValuePageData_Item.data.put("flash", localTagNode.toHtml());
      this.mAdapter.addHtmlNode(localIKValuePageData_Item);
      checkTextNode();
    }
  }

  public void parseHtml(String paramString)
    throws ParserException
  {
    this.mHtmlParser = new Parser(new Lexer(paramString));
    NodeList localNodeList = this.mHtmlParser.parse(null);
    int i = 0;
    if (i >= localNodeList.size())
      return;
    Node localNode = localNodeList.elementAt(i);
    if (localNode instanceof Html)
      parseHtmlNode((Html)localNode);
    while (true)
    {
      while (true)
      {
        while (true)
          ++i;
        if (!(localNode instanceof ParagraphTag))
          break;
        parsePTag((ParagraphTag)localNode);
      }
      parseChildNode(localNode);
    }
  }
}